﻿package mortal.game.scene3D.player.head
{
    import __AS3__.vec.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import flash.text.*;
    import frEngine.pools.*;
    import mortal.common.global.*;
    import mortal.common.tools.*;
    import mortal.game.scene3D.display3d.blood.*;
    import mortal.game.scene3D.display3d.icon3d.*;
    import mortal.game.scene3D.display3d.text3d.dynamicText3d.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.common.*;

    public class HeadContainer extends Pivot3D
    {
        protected var _bloodVisible:Boolean = true;
        protected var _bloodType:int = 3;
        protected var blood:Blood3D;
        protected var _nameVisible:Boolean = true;
        protected var text3D:Text3D;
        protected var _levelText3D:Text3D;
        private var _isBoss:Boolean = false;
        protected var _levelVisible:Boolean = true;
        protected var _guildVisible:Boolean = true;
        protected var guildText3D:Text3D;
        protected var wineLifeText3D:Text3D;
        protected var bossBelong:Icon3D;
        protected var qqHuangzhuan:QQHuangzhuan;
        protected var wines:Vector.<Icon3D>;
        protected var _treasureBox:Icon3D;
        protected var _guildTreeVisible:Boolean = false;
        protected var guildTreeText3D:Text3D;
        protected var _leftTimeVisible:Boolean = false;
        protected var leftTimeText3D:Text3D;
        protected var _titleVisible:Boolean = true;
        protected var title:HeadTitle;
        protected var _specialTitleVisible:Boolean = true;
        protected var _specialTitle:EffectPlayer;
        private var _isLayout:Boolean = true;
        private var _titleId:int;
        private static var textField:TextField;

        public function HeadContainer()
        {
            this.wines = new Vector.<Icon3D>;
            super("");
            createTextField();
            return;
        }// end function

        public function get hangBoneName() : String
        {
            return "nameHangBone";
        }// end function

        public function addSkillName(param1:TextField) : void
        {
            var _loc_2:* = Text3DFactory.instance.createtext3D(param1, true, this);
            _loc_2.x = -int(_loc_2.textWidth / 2) + 2;
            _loc_2.y = 60;
            return;
        }// end function

        public function updateLife(param1:Number, param2:Number, param3:int = 3, param4:Boolean = true) : void
        {
            if (param1 > param2)
            {
                param1 = param2;
            }
            if (this._bloodType != param3 && this.blood)
            {
                this.blood.dispose();
                this.blood = null;
            }
            this._bloodType = param3;
            if (!this.blood)
            {
                this.blood = Blood3DResource.instance.createBlood3D(param3);
            }
            var _loc_5:Number = 0;
            if (param2 > 0)
            {
                _loc_5 = param1 / param2;
            }
            this.blood.bloodRate = _loc_5;
            this.addChild(this.blood);
            this.setLayout();
            return;
        }// end function

        private function updateText(param1:Text3D, param2:String) : Text3D
        {
            textField.htmlText = param2;
            if (!param1)
            {
                param1 = Text3DFactory.instance.createtext3D(textField, false, this);
            }
            else
            {
                Text3DFactory.instance.updateText3D(param1, textField, this);
            }
            return param1;
        }// end function

        public function updateName(param1:String) : void
        {
            this.text3D = this.updateText(this.text3D, param1);
            this.setLayout();
            return;
        }// end function

        public function updateLevel(param1:int, param2:Boolean = false) : void
        {
            var _loc_3:* = GameMapUtil.isInHideLevelMap && !param2 ? (" ?") : (param1.toString());
            this._isBoss = param2;
            this._levelText3D = this.updateText(this._levelText3D, _loc_3);
            this.setLayout();
            return;
        }// end function

        public function updateLeftTime(param1:String) : void
        {
            this.leftTimeText3D = this.updateText(this.leftTimeText3D, param1.toString());
            this.setLayout();
            return;
        }// end function

        public function updateBossBelong(param1:Boolean) : void
        {
            if (param1)
            {
                if (!this.bossBelong)
                {
                    this.bossBelong = Icon3DFactory.instance.createOtherIcon3D("otherIcons2.png", EotherIcons2Type.Boss);
                    this.addChild(this.bossBelong);
                }
            }
            else if (this.bossBelong)
            {
                Icon3DFactory.instance.disposeIcon3D(this.bossBelong);
                this.bossBelong = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateQQHuangzhuan(param1:int, param2:int, param3:Boolean, param4:Boolean = false, param5:Boolean = false) : void
        {
            if (param2 > 0 && (param1 == 1 || param1 == 2))
            {
                if (!this.qqHuangzhuan)
                {
                    this.qqHuangzhuan = FrObjectPool.getObject(QQHuangzhuan, this, "");
                }
                this.qqHuangzhuan.updateQQHuangzhuan(param1, param2, param3, param4, param5);
                this.addChild(this.qqHuangzhuan);
            }
            else if (this.qqHuangzhuan)
            {
                this.qqHuangzhuan.dispose();
                this.qqHuangzhuan = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateWineIcons(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Icon3D = null;
            _loc_2 = 0;
            while (_loc_2 < this.wines.length)
            {
                
                Icon3DFactory.instance.disposeIcon3D(this.wines[_loc_2]);
                _loc_2++;
            }
            this.wines.length = 0;
            _loc_2 = 0;
            while (_loc_2 < param1.length)
            {
                
                _loc_3 = _loc_2 > 2 ? (2) : (_loc_2);
                _loc_4 = Icon3DFactory.instance.createOtherIcon3D("otherIcons2.png", EotherIcons2Type["WineIcons" + _loc_3]);
                this.wines[_loc_2] = _loc_4;
                this.addChild(_loc_4);
                _loc_2++;
            }
            this.setLayout();
            return;
        }// end function

        public function updateTreasureIcon(param1:int) : void
        {
            if (this._treasureBox)
            {
                Icon3DFactory.instance.disposeIcon3D(this._treasureBox);
                this._treasureBox = null;
            }
            if (param1 > 0 && param1 < 4)
            {
                switch(param1)
                {
                    case 1:
                    {
                        this._treasureBox = Icon3DFactory.instance.createOtherIcon3D("otherIcons2.png", EotherIcons2Type.TreasureIcon2);
                        break;
                    }
                    case 2:
                    {
                        this._treasureBox = Icon3DFactory.instance.createOtherIcon3D("otherIcons2.png", EotherIcons2Type.TreasureIcon0);
                        break;
                    }
                    case 3:
                    {
                        this._treasureBox = Icon3DFactory.instance.createOtherIcon3D("otherIcons2.png", EotherIcons2Type.TreasureIcon1);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                this.addChild(this._treasureBox);
            }
            this.setLayout();
            return;
        }// end function

        public function updateGuildName(param1:String) : void
        {
            if (param1)
            {
                this.guildText3D = this.updateText(this.guildText3D, param1);
            }
            else if (this.guildText3D)
            {
                Text3DFactory.instance.disposeText3D(this.guildText3D);
                this.guildText3D = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateWineLife(param1:String) : void
        {
            if (param1)
            {
                this.wineLifeText3D = this.updateText(this.wineLifeText3D, param1);
                this.wineLifeText3D.show();
            }
            else if (this.wineLifeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.wineLifeText3D);
                this.wineLifeText3D = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateGuildTreeInfo(param1:String) : void
        {
            if (param1)
            {
                this.guildTreeText3D = this.updateText(this.guildTreeText3D, param1);
            }
            else if (this.guildTreeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.guildTreeText3D);
                this.guildTreeText3D = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateTitle(param1:int) : void
        {
            this._titleId = param1;
            this.drawTitle();
            return;
        }// end function

        private function drawTitle() : void
        {
            var _loc_1:* = this._titleVisible ? (this._titleId) : (0);
            if (_loc_1)
            {
                if (!this.title)
                {
                    this.title = FrObjectPool.getObject(HeadTitle, this);
                }
                this.title.updateTitle(_loc_1);
                this.addChild(this.title);
            }
            else if (this.title)
            {
                this.title.clear();
                this.title.dispose();
                this.title = null;
            }
            this.setLayout();
            return;
        }// end function

        public function updateSpecialTitle(param1:String) : void
        {
            if (param1)
            {
                if (this._specialTitle)
                {
                    this._specialTitle.dispose();
                }
                this._specialTitle = EffectPlayerPool.instance.getEffectPlayer(param1, this, null, false, true);
                this._specialTitle.play(PlayMode.ANIMATION_LOOP_MODE);
            }
            else if (this._specialTitle)
            {
                this._specialTitle.dispose();
                this._specialTitle = null;
            }
            this.setLayout();
            return;
        }// end function

        private function setLayout() : void
        {
            if (this._isLayout == true)
            {
                this._isLayout = false;
                Global.instance.callLater(this.layout);
            }
            return;
        }// end function

        private function layout() : void
        {
            var _loc_3:int = 0;
            var _loc_1:Number = 0;
            var _loc_2:Number = 0;
            if (this.blood && this._bloodVisible)
            {
                this.blood.y = _loc_2;
                if (DebugConst.isOutPutChangeAction)
                {
                    Log.debug("HeadCotainer位置blood", _loc_2);
                }
                _loc_2 = _loc_2 + 10;
                if (this._levelText3D && this._levelText3D.parent && this._levelVisible)
                {
                    if (this._isBoss)
                    {
                        this._levelText3D.x = -87 - this._levelText3D.textWidth / 2;
                        this._levelText3D.y = _loc_2 - 21;
                    }
                    else
                    {
                        this._levelText3D.x = -52 - this._levelText3D.textWidth / 2;
                        this._levelText3D.y = _loc_2 - 22;
                    }
                }
            }
            if (this.text3D && this._nameVisible)
            {
                _loc_1 = this.text3D.textWidth;
                this.text3D.y = _loc_2;
                if (this.bossBelong)
                {
                    _loc_1 = _loc_1 + 27;
                }
                if (this.qqHuangzhuan)
                {
                    _loc_1 = _loc_1 + 27;
                }
                if (this.qqHuangzhuan)
                {
                    this.qqHuangzhuan.x = -int(_loc_1 / 2) + 2;
                    this.qqHuangzhuan.y = _loc_2 + 8;
                    this.text3D.x = this.qqHuangzhuan.x + this.qqHuangzhuan.width + 5;
                    this.text3D.y = _loc_2 + 2;
                }
                else
                {
                    this.text3D.x = -int(_loc_1 / 2) + 2;
                }
                if (this.bossBelong)
                {
                    this.bossBelong.x = this.text3D.x + this.text3D.textWidth + 10;
                    this.bossBelong.y = _loc_2 + 14;
                }
                if (DebugConst.isOutPutChangeAction)
                {
                    Log.debug("HeadCotainer位置name", _loc_2);
                }
                _loc_2 = _loc_2 + 20;
            }
            if (this._guildVisible && this.guildText3D)
            {
                this.guildText3D.x = -int(this.guildText3D.textWidth / 2) + 2;
                this.guildText3D.y = _loc_2;
                if (DebugConst.isOutPutChangeAction)
                {
                    Log.debug("HeadCotainer位置guild", _loc_2);
                }
                _loc_2 = _loc_2 + 20;
            }
            if (this.wineLifeText3D && !this.wineLifeText3D.isHide)
            {
                this.wineLifeText3D.x = -int(this.wineLifeText3D.textWidth / 2) + 2;
                this.wineLifeText3D.y = _loc_2;
                _loc_2 = _loc_2 + 20;
            }
            if (this._guildTreeVisible && this.guildTreeText3D)
            {
                this.guildTreeText3D.x = -int(this.guildTreeText3D.textWidth / 2) + 2;
                this.guildTreeText3D.y = this.text3D.y - this.guildTreeText3D.textHeight - 5;
                _loc_2 = _loc_2 + 20;
            }
            if (this.wines.length)
            {
                _loc_3 = 0;
                while (_loc_3 < this.wines.length)
                {
                    
                    this.wines[_loc_3].x = (this.wines.length - 1) * -20 + _loc_3 * 40;
                    this.wines[_loc_3].y = _loc_2 - 7;
                    _loc_3++;
                }
                _loc_2 = _loc_2 + 20;
            }
            if (this._treasureBox)
            {
                this._treasureBox.y = _loc_2 + 43;
                _loc_2 = _loc_2 + 20;
            }
            if (this._leftTimeVisible && this.leftTimeText3D)
            {
                if (this._treasureBox && this._treasureBox.parent)
                {
                    this.leftTimeText3D.y = _loc_2 + 65;
                }
                else
                {
                    this.leftTimeText3D.y = _loc_2;
                }
                this.leftTimeText3D.x = -int(this.leftTimeText3D.textWidth / 2) + 2;
                _loc_2 = _loc_2 + 20;
            }
            if (this.title && this._titleVisible)
            {
                this.title.x = 0;
                this.title.y = _loc_2;
                if (DebugConst.isOutPutChangeAction)
                {
                    Log.debug("HeadCotainer位置title", _loc_2);
                }
                _loc_2 = _loc_2 + (this.title.height + 20);
            }
            if (this._specialTitleVisible && this._specialTitle)
            {
                this._specialTitle.x = 0;
                this._specialTitle.y = _loc_2 + 15;
                _loc_2 = _loc_2 + 32;
            }
            this._isLayout = true;
            return;
        }// end function

        public function disposeLevel3D() : void
        {
            if (this._levelText3D)
            {
                Text3DFactory.instance.disposeText3D(this._levelText3D);
                this._levelText3D = null;
            }
            return;
        }// end function

        public function disposeWineLifeText() : void
        {
            if (this.wineLifeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.wineLifeText3D);
                this.wineLifeText3D = null;
            }
            return;
        }// end function

        public function clear() : void
        {
            var _loc_1:int = 0;
            this._isBoss = false;
            this._bloodType = EBloodType.Monster;
            if (this.blood)
            {
                this.blood.dispose();
                this.blood = null;
            }
            if (this._levelText3D)
            {
                Text3DFactory.instance.disposeText3D(this._levelText3D);
                this._levelText3D = null;
            }
            if (this.text3D)
            {
                Text3DFactory.instance.disposeText3D(this.text3D);
                this.text3D = null;
            }
            if (this.guildText3D)
            {
                Text3DFactory.instance.disposeText3D(this.guildText3D);
                this.guildText3D = null;
            }
            if (this.wineLifeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.wineLifeText3D);
                this.wineLifeText3D = null;
            }
            if (this.guildTreeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.guildTreeText3D);
                this.guildTreeText3D = null;
            }
            if (this.leftTimeText3D)
            {
                Text3DFactory.instance.disposeText3D(this.leftTimeText3D);
                this.leftTimeText3D = null;
            }
            if (this.title)
            {
                this.title.clear();
                this.title = null;
            }
            if (this._specialTitle)
            {
                this._specialTitle.dispose();
                this._specialTitle = null;
            }
            if (this.bossBelong)
            {
                Icon3DFactory.instance.disposeIcon3D(this.bossBelong);
                this.bossBelong = null;
            }
            if (this.qqHuangzhuan)
            {
                this.qqHuangzhuan.dispose();
                this.qqHuangzhuan = null;
            }
            if (this.wines.length)
            {
                _loc_1 = this.wines.length - 1;
                while (_loc_1 >= 0)
                {
                    
                    Icon3DFactory.instance.disposeIcon3D(this.wines[_loc_1]);
                    _loc_1 = _loc_1 - 1;
                }
                this.wines.length = 0;
            }
            if (this._treasureBox)
            {
                Icon3DFactory.instance.disposeIcon3D(this._treasureBox);
                this._treasureBox = null;
            }
            this._bloodVisible = true;
            this._nameVisible = true;
            this._guildVisible = true;
            this._guildTreeVisible = false;
            this._leftTimeVisible = false;
            this._titleVisible = true;
            this._specialTitleVisible = true;
            this._isLayout = true;
            this._levelVisible = true;
            return;
        }// end function

        public function show() : void
        {
            this.updateBloodVisible(true);
            this.updateNameVisible(true);
            this.updateGuildVisible(true);
            this.updateTitleVisible(true);
            this.updateSpecialTitleVisible(true);
            return;
        }// end function

        public function hide() : void
        {
            this.updateBloodVisible(false);
            this.updateNameVisible(false);
            this.updateGuildVisible(false);
            this.updateTitleVisible(false);
            this.updateSpecialTitleVisible(false);
            return;
        }// end function

        public function updateBloodVisible(param1:Boolean = true, param2:Boolean = true) : void
        {
            if (param2)
            {
                this._bloodVisible = param1;
            }
            if (this.blood)
            {
                if (param1)
                {
                    this.blood.show();
                }
                else
                {
                    this.blood.hide();
                }
            }
            if (this._levelText3D)
            {
                if (param1 && this._levelVisible)
                {
                    this._levelText3D.show();
                }
                else
                {
                    this._levelText3D.hide();
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateNameVisible(param1:Boolean = true) : void
        {
            this._nameVisible = param1;
            if (this.text3D)
            {
                if (param1)
                {
                    this.text3D.show();
                    if (this.bossBelong)
                    {
                        this.bossBelong.show();
                    }
                    if (this.qqHuangzhuan)
                    {
                        this.qqHuangzhuan.show();
                    }
                }
                else
                {
                    this.text3D.hide();
                    if (this.bossBelong)
                    {
                        this.bossBelong.hide();
                    }
                    if (this.qqHuangzhuan)
                    {
                        this.qqHuangzhuan.hide();
                    }
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateGuildVisible(param1:Boolean = true) : void
        {
            this._guildVisible = param1;
            if (this.guildText3D)
            {
                if (param1)
                {
                    this.guildText3D.show();
                }
                else
                {
                    this.guildText3D.hide();
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateGuildTreeVisible(param1:Boolean = true) : void
        {
            this._guildTreeVisible = param1;
            if (this.guildTreeText3D)
            {
                if (param1)
                {
                    this.guildTreeText3D.show();
                }
                else
                {
                    this.guildTreeText3D.hide();
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateLeftTimeVisible(param1:Boolean = true) : void
        {
            this._leftTimeVisible = param1;
            if (this.leftTimeText3D)
            {
                if (param1)
                {
                    this.leftTimeText3D.show();
                }
                else
                {
                    this.leftTimeText3D.hide();
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateTitleVisible(param1:Boolean = true) : void
        {
            this._titleVisible = param1;
            this.drawTitle();
            this.setLayout();
            return;
        }// end function

        public function updateSpecialTitleVisible(param1:Boolean = true) : void
        {
            this._specialTitleVisible = param1;
            if (this._specialTitle)
            {
                if (param1)
                {
                    if (!this._specialTitle.parent)
                    {
                        this.addChild(this._specialTitle);
                    }
                }
                else if (this._specialTitle.parent)
                {
                    this.removeChild(this._specialTitle);
                }
            }
            this.setLayout();
            return;
        }// end function

        public function updateLevelVisible(param1:Boolean) : void
        {
            this._levelVisible = param1;
            if (this._levelText3D)
            {
                if (param1)
                {
                    this._levelText3D.show();
                }
                else
                {
                    this._levelText3D.hide();
                }
            }
            this.setLayout();
            return;
        }// end function

        public function get bloodVisible() : Boolean
        {
            return this._bloodVisible;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this.clear();
            super.dispose(param1);
            return;
        }// end function

        private static function createTextField() : void
        {
            if (!textField)
            {
                textField = UIFactory.textField("", 0, 0, -1, -1, null, GlobalStyle.textFormatItemWhite);
                textField.autoSize = TextFieldAutoSize.LEFT;
            }
            return;
        }// end function

    }
}
